package graph.day16;

import java.util.Arrays;
import java.util.Scanner;

public class Prim {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int v = sc.nextInt();
        int e = sc.nextInt();

        int[][] grid = new int[v + 1][v + 1];
        for (int i = 0; i <= v; i++) {
            Arrays.fill(grid[i], 10001);
        }

        for (int i = 0; i < e; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            int k = sc.nextInt();
            grid[x][y] = k;
            grid[y][x] = k;
        }

        int[] minDist = new int[v + 1];
        Arrays.fill(minDist, 10001);
        boolean[] visited = new boolean[v + 1];
        for (int i = 1; i < v; i++) {
            int cur = -1;
            int minVal = Integer.MAX_VALUE;
            for (int j = 1; j <= v; j++) {
                if (!visited[j] && minDist[j] < minVal) {
                    cur = j;
                    minVal = minDist[j];
                }
            }
            visited[cur] = true;
            for (int j = 1; j <= v; j++) {
                if (!visited[j] && grid[cur][j] < minDist[j]) {
                    minDist[j] = grid[cur][j];
                }
            }
        }
        int result = 0;
        for (int i = 2; i <= v; i++) {
            result += minDist[i];
        }
        System.out.println(result);
    }
}
